package com.dlwx.firefighting.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.firefighting.R;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.inter.PayPswInter;
import com.dlwx.firefighting.utils.DialogUtils;
import com.dlwx.firefighting.utils.PayPswDialog;
import com.dlwx.firefighting.utils.SPUtils;
import com.dlwx.firefighting.utils.ToastUtils;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PayCodeActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.et_money)
    EditText etMoney;
    @BindView(R.id.et_remark)
    EditText etRemark;
    @BindView(R.id.tv_pay)
    TextView tvPay;
    private String content;
    private boolean isPay=false;
    private String cashierId;
    private String cashierPhone;
    private String cashierName;
    private String userId;
    private String money;
//    收银员0,会员1
    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_code);
        ButterKnife.bind(this);
        content = getIntent().getStringExtra("content");
        type = getIntent().getStringExtra("type");
        userId = (String) SPUtils.get(this, Constant.USER_ID, "");
        dealContent(content);
        initView();
    }

    private void dealContent(String content) {
        String[] str = new String[3];
        if(!TextUtils.isEmpty(content)){
            str=content.split("&");
        }
        String[] ids=str[0].split("=");
        if(ids.length<1){
            return;
        }
        String[] names=str[1].split("=");
        if(names.length<1){
            return;
        }
        cashierId=str[0].split("=")[1];
        cashierName=str[1].split("=")[1];
//        cashierPhone=str[2].split("=")[1];
    }

    @Override
    protected void onResume() {
        super.onResume();
        isPay= (boolean) SPUtils.get(this, Constant.IS_PAY,false);
    }

    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("支付");
        tvName.setText("收款人："+cashierName);

    }

    @OnClick({R.id.title_left_one_btn, R.id.tv_middle_title, R.id.tv_pay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                break;
            case R.id.tv_middle_title:
                break;
            case R.id.tv_pay:
                money=etMoney.getText().toString();
                if(TextUtils.isEmpty(money)){
                    ToastUtils.toastForShort(PayCodeActivity.this,"金额不能为空");
                    return;
                }
                doPayPsw();
                break;
        }
    }

    private void doPayPsw() {
        if(!isPay){
            final NormalDialog dialog = DialogUtils.getNormalDialog(this, "提示", "设置支付密码");
            dialog.setOnBtnClickL(new OnBtnClickL() {
                @Override
                public void onBtnClick() {
                    dialog.dismiss();
                }
            }, new OnBtnClickL() {
                @Override
                public void onBtnClick() {
                    dialog.dismiss();
                    startActivity(new Intent(PayCodeActivity.this,PayPswActivity.class));
                }
            });
            return;
        }
        String noPswMoney= (String) SPUtils.get(PayCodeActivity.this, Constant.IS_NO_PSW_MONEY, "");
        boolean noPsw= (boolean) SPUtils.get(PayCodeActivity.this, Constant.IS_NO_PSW, false);
        String psw_str= (String) SPUtils.get(PayCodeActivity.this,Constant.PAY_PASSWORD,"");
        if(noPsw){
//            免密支付
            if(!TextUtils.isEmpty(noPswMoney)){
                try {
                    int noMoney=Integer.valueOf(noPswMoney);
                    int m=Integer.valueOf(money);
                    if(m<noMoney||m==noMoney){
                        if(TextUtils.equals("0",type)){
                            doPayCashier(psw_str);
                        }else if(TextUtils.equals("1",type)){
                            doPayMember(psw_str);
                        }
                        return;
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }

            }
        }

        PayPswDialog mDialog = new PayPswDialog(this, new PayPswInter() {
            @Override
            public void getPsw(String psw) {
                if(TextUtils.equals("0",type)){
                    doPayCashier(psw);
                }else if(TextUtils.equals("1",type)){
                    doPayMember(psw);
                }
            }
        });
        mDialog.show();
    }

    private void doPayCashier(String psw) {
        DialogUtils.showProgressDlg(this,"");
        OkGo.<String>post(Constant.PAY)
                .tag(this)
                .params("user_id", userId)
                .params("cashier_id", cashierId)
                .params("money", money)
                .params("pay_password", psw)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        DialogUtils.stopProgressDlg();
//                        DealResult dealResult = new DealResult();
//                        BasicBean basicBean=dealResult.dealBasic(PayCodeActivity.this,response);
//                        if (basicBean == null) {
//                            return;
//                        }
//                        PayResBean bean = dealResult.dealOne(PayCodeActivity.this, response, new TypeToken<BaseOneBean<PayResBean>>() {
//                        }.getType());
//                        if (bean == null) {
//                            return;
//                        }
//                        ToastUtils.toastForShort(PayCodeActivity.this, "支付成功");
//                        Intent i = new Intent(PayCodeActivity.this, PayResultActivity.class);
//                        i.putExtra("bean", bean);
//                        startActivity(i);
//                        PayCodeActivity.this.finish();
                    }
                    @Override
                    public void onError(Response<String> response) {
                        DialogUtils.stopProgressDlg();
                        ToastUtils.toastForShort(PayCodeActivity.this, getString(R.string.bad_net));
                        super.onError(response);
                    }
                });

    }
    private void doPayMember(String psw) {
        DialogUtils.showProgressDlg(this,"");
        OkGo.<String>post(Constant.TRANSFER_ACCOUNTS)
                .tag(this)
                .params("pay_password", psw)
                .params("user_id", userId)
                .params("mobile_phone", cashierId)
                .params("money", money)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        DialogUtils.stopProgressDlg();
//                        DealResult dealResult = new DealResult();
//                        BasicBean basicBean=dealResult.dealBasic(PayCodeActivity.this,response);
//                        if (basicBean == null) {
//                            return;
//                        }
//                       /* PayResBean bean = dealResult.dealOne(PayCodeActivity.this, response, new TypeToken<BaseOneBean<PayResBean>>() {
//                        }.getType());
//                        if (bean == null) {
//                            return;
//                        }*/
//                        PayResBean bean=new PayResBean();
//                        float mm=Float.parseFloat(money);
//                        bean.setPay_money(mm);
//                        bean.setGive_integral(0);
//                        bean.setPay_integral(0);
//                        ToastUtils.toastForShort(PayCodeActivity.this, "支付成功");
//                        Intent i = new Intent(PayCodeActivity.this, PayResultActivity.class);
//                        i.putExtra("bean", bean);
//                        startActivity(i);
//                        PayCodeActivity.this.finish();
                    }
                    @Override
                    public void onError(Response<String> response) {
                        DialogUtils.stopProgressDlg();
                        ToastUtils.toastForShort(PayCodeActivity.this, getString(R.string.bad_net));
                        super.onError(response);
                    }
                });

    }
//        Intent i = new Intent(PayCodeActivity.this, PayResultActivity.class);
//        startActivity(i);
//        PayCodeActivity.this.finish();

}
